
%  adlpr.m
%            31 March 1998
% Adaptive Prediction with Adaline

clear

% Input signal x(t)

f1 = 2 ; % kHz
         % f2 = 4*f1 = 8kHz
         % fs = 10*f2 = 80 kHz -- sampling frequency
ts = 1/(40*f1) ; % 12.5 usec -- sampling time
N = 100 ;
t1 = (0:N)*4*ts ;
t2 = (0:2*N)*ts + 4*(N+1)*ts ;
t = [t1 t2] ;     % 0 to 7.5 sec
N = size(t, 2) ;  % N = 302

xt = [sin(2*pi*f1*t1) sin(2*pi*2*f1*t2)];
plot(t, xt), grid, title('Signal to be predicted')

p = 4 ; % Number of synapses

% formation of the input matrix X of size  p by N
% use the convolution matrix. Try  convmtx(1:8, 5)

X = convmtx(xt, p) ; X = X(:, 1:N) ;

d = xt ;  % The target signal is equal to the input signal

 y  = zeros(size(d)) ;   % memory allocation for y
eps = zeros(size(d)) ;   % memory allocation for eps

eta = 0.4   ; % learning rate/gain
w = rand(1, p) ; % Initialisation of the weight vector

for n = 1:N  %  learning loop

 y(n) = w*X(:,n) ;      % predicted output signal
 eps(n) = d(n) - y(n) ; % error signal
 w = w + eta*eps(n)*X(:,n)' ;
end

figure(1)
plot(t, d, 'b', t, y, '-r'), grid, ...
title('target and predicted signals'), xlabel('time [sec]')
% print -f1 -deps adlpsg

figure(2)
plot(t, eps), grid, title('prediction error'), xlabel('time [sec]')
%  print -f2 -deps adlper
